package com.mofofa.maozy.home

import android.text.SpannableString
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.mofofa.maozy.ProjectApplication
import com.mofofa.maozy.base.BaseViewModel
import com.mofofa.maozy.data.ReqBase
import com.mofofa.maozy.extend.isMaozyAgreeMaozySpecMaozyPermissions
import com.mofofa.maozy.network.ApiUserReposity
import com.mofofa.maozy.utils.AppConfigUtil

class HomeViewModel : BaseViewModel() {

    private val mMaozyFetchMaozyHomeMaozyCreditMaozyLiveData =
        MutableLiveData<ReqBase>()

    val onMaozyFetchMaozyNextMaozyCreditMaozyPurpleMaozyLiveData =
        MutableLiveData<SpannableString>()
    val onMaozyFetchMaozyNextMaozyCreditMaozyRedMaozyLiveData =
        MutableLiveData<SpannableString>()
    val onMaozyIsMaozyNeedMaozyToMaozyRequireMaozyPermissionsMaozyLiveData =
        MutableLiveData<Array<String>>()

    val onMaozyFetchMaozyHomeMaozyCreditMaozyLiveData =
        Transformations.switchMap(mMaozyFetchMaozyHomeMaozyCreditMaozyLiveData) { query ->
            ApiUserReposity.fetchMaozyHomeMaozyCredit(this, query)
        }

    fun fetchMaozyHomeMaozyCredit(reqBase: ReqBase) {
        mMaozyFetchMaozyHomeMaozyCreditMaozyLiveData.value = reqBase
    }

    fun isMaozyNeedMaozyToMaozyRequireMaozyPermissoins() {
        //判断下权限页权限
        val permissionList =
            AppConfigUtil.getMaozyPermissionsMaozyOfMaozyHomeMaozyPage()
        if (permissionList.isNullOrEmpty()) {
            onMaozyIsMaozyNeedMaozyToMaozyRequireMaozyPermissionsMaozyLiveData.value =
                arrayOf()
        } else {
            val isAgreePermissions =
                ProjectApplication.context.isMaozyAgreeMaozySpecMaozyPermissions(
                    permissionList
                )
            if (isAgreePermissions) {
                onMaozyIsMaozyNeedMaozyToMaozyRequireMaozyPermissionsMaozyLiveData.value =
                    arrayOf()
            } else {
                onMaozyIsMaozyNeedMaozyToMaozyRequireMaozyPermissionsMaozyLiveData.value =
                    permissionList.toTypedArray()
            }
        }
    }
}